﻿using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco;
using umbraco.cms.businesslogic.datatype;
using umbraco.DataLayer;
using umbraco.interfaces;

namespace netaddICTs.Umb.ProtectedMedia.Datatypes.ProtectedUploadField {
    
    public class PrevalueEditor : PlaceHolder, IDataPrevalue {
    
        private readonly BaseDataType _baseDatatype;

        public PrevalueEditor(BaseDataType baseDataType) {
            _baseDatatype = baseDataType;
        }

        #region IDataPrevalue Members

        public Control Editor {
            get { return this; }
        }

        public void Save() {
            var helper = DataLayerHelper.CreateSqlHelper(GlobalSettings.DbDSN);

            _baseDatatype.DBType = DBTypes.Ntext;

            helper.ExecuteNonQuery(
                "DELETE FROM cmsDataTypePreValues WHERE datatypenodeid = @dtdefid",
                helper.CreateParameter("@dtdefid", _baseDatatype.DataTypeDefinitionId));

            helper.ExecuteNonQuery(
                "INSERT INTO cmsDataTypePreValues (datatypenodeid, [value], sortorder, alias) VALUES (@dtdefid, @value, 0, '')",
                helper.CreateParameter("@value", string.Empty),
                helper.CreateParameter("@dtdefid", _baseDatatype.DataTypeDefinitionId));
        }

        #endregion
    }
}